#include "bor_add_dialog.h"
#include "ui_bor_add_dialog.h"
#include"QDebug"
#include"QDate"
bor_add_Dialog::bor_add_Dialog(QWidget *parent) :
    QDialog(parent),
    borr({"","","","",""}),
    flag(false),
//    book({"","",""}),
//    student({"","","","",""}),
    ui(new Ui::bor_add_Dialog)
{
    ui->setupUi(this);
    ui->comboBox->setDisabled(true);

}

bor_add_Dialog::~bor_add_Dialog()
{
    delete ui;
}

QString bor_add_Dialog::getState(int i)
{
//    Lending = 1,//借出
//    Not_Lending = 2,//未借出
//    Lost = 3, //丢失
//    Destroyed = 4,//毁坏
//    Damage = 5//破损
    switch (i) {
        case 1 : return QString("已借出");
        case 2 : return QString("未借出");
        case 3 : return QString("丢失");
        case 4 : return QString("毁坏");
        case 5 : return QString("破损");
    }
}

void bor_add_Dialog::on_lineEdit_stu_number_editingFinished()
{
    QString stu = ui->lineEdit_stu_number->text();
    emit stu_number(stu);
    if(student.size() == 0) return;
    ui->lineEdit_stu_name->setText(student[1]);
    ui->lineEdit_stu_class->setText(student[3]);

//        qDebug()<<student;
}

void bor_add_Dialog::on_lineEdit_book_number_editingFinished()
{
    QString bok = ui->lineEdit_book_number->text();
    emit book_number(bok);
    if(book.size() == 0) return;

    ui->lineEdit_book_name->setText(book[2]);
    int cnt = 1;
    book_i = 9;
    for(; book_i < book.size();book_i+=3)
    {
        if(book[book_i] == "2")
        {
            cnt = 2;
            break;
        }
    }

    ui->comboBox->addItem(getState(cnt));

//    if(cnt == 2)
//    {
//        this->borr[0] = student[0] + book[i-1];
//        this->borr[1] = student[0];
//        this->borr[2] = book[0];
//        this->borr[3] = QDate::currentDate().toString("yyyy年MM月dd日");
//        this->borr[4] = QDate::currentDate().addMonths(1).toString("yyyy年MM月dd日");
//        student[4] = QString::number(student[4].toInt() + 1);
//        book[i] = "1";
//    }

//    qDebug()<<borr<<endl;
}

void bor_add_Dialog::on_pushButton_clicked()
{
    if(ui->comboBox->currentText() == "未借出")
    {
        this->borr[0] = student[0] + book[book_i-1];
        this->borr[1] = student[0];
        this->borr[2] = book[0];
        this->borr[3] = QDate::currentDate().toString("yyyy年MM月dd日");
        this->borr[4] =  QDate(1000,1,1).toString("yyyy年MM月dd日");
        student[4] = QString::number(student[4].toInt() + 1);
        book[book_i] = "1";
        book[book_i + 1] = "";
        flag = true;
    }
    this->close();
//    qDebug()<<borr<<endl;
}

void bor_add_Dialog::keyPressEvent(QKeyEvent *event)
{
    if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {
                // 忽略回车键事件
                event->ignore();
            } else {
                // 对于其他按键，调用基类的keyPressEvent
                QDialog::keyPressEvent(event);
    }
}

void bor_add_Dialog::closeEvent(QCloseEvent *event)
{
    this->close();
}

void bor_add_Dialog::on_pushButton_2_clicked()
{
    this->close();
}
